/**
 * 
 */
package com.taobao.wolfwolf.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

/**
 * @author qinanhg@gmail.com
 * 
 *         创建时间: 2011-4-25 上午11:16:19
 */
public class Util {
	// 分页大小
	public static final int pageSize = 20;

	// 商品图片的下载地址
	public static final String pictureDownPath = "picture/";

	// 表格中图片的宽
	public static final int IMAGEWIDTH = 128;

	// 表格中图片的高
	public static final int IMAGEHEIGHT = 128;
	
	public static final String WAIT_BUYER_PAY = "WAIT_BUYER_PAY";
	public static final String WAIT_SELLER_SEND_GOODS = "WAIT_SELLER_SEND_GOODS";
	public static final String WAIT_BUYER_CONFIRM_GOODS = "WAIT_BUYER_CONFIRM_GOODS";
	
	
	

	/**
	 * 查找节点名称的索引值
	 * 
	 * @param TreeNodeName
	 *            节点名称
	 * @return 找到的节点索引
	 */
	public static int getTreeNodeIndex(String TreeNodeName) {
		String[] str = { "店铺信誉", "商品管理", "淘宝登陆" };

		for (int i = 0; i < str.length; i++) {
			if (str[i].equals(TreeNodeName))
				return i;
		}

		return -1;
	}
	
	public static int getFieldPostionX(String position) {
		String[] str = position.split(":");
		return Integer.valueOf(str[0]);
	}
	
	public static int getFieldPostionY(String position) {
		String[] str = position.split(":");
		return Integer.valueOf(str[1]);
	}
	
	public static int getFieldPostionWidth(String position) {
		String[] str = position.split(":");
		return Integer.valueOf(str[2]);
	}
	
	/**
	 * 下载图片
	 * 
	 * @param url
	 * @param file
	 * @throws IOException
	 */
	public static void download2(String url, String file) throws IOException {

		// URL实例化，其中的newUrl是图片的Url
		URL u = new URL(url);
		HttpURLConnection imageconnection = (HttpURLConnection) u
				.openConnection();
		long startTime = System.currentTimeMillis();// 现在时间
		int readBytes = 0;
		System.out.println("StartTime is ..." + startTime);
		long length = imageconnection.getContentLength();// .getContentLength()此连接
		byte[] buffer = new byte[4096];
		InputStream inputStream = imageconnection.getInputStream();
		int read = 0;
		System.out.println("length=" + length);
		FileOutputStream fileOutput = new FileOutputStream(file);
		while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
			fileOutput.write(buffer, 0, read);
			System.out.println((int) ((double) readBytes / length * 100));
			readBytes += read;
		}
		System.out.println("下载图片完成");
	}

	/**
	 * add: 0 delete: 1 condition: 2 refresh: 3 sync: 4 next: 5 previous: 6
	 * first: 7 end: 8 search: 9
	 * 
	 * @param JButtonActionString
	 * @return
	 */
	public static int getJButtonIndex(String JButtonActionString) {
		String[] str = { "add", "delete", "condition", "refresh", "sync",
				"next", "previous", "first", "end", "search" };

		for (int i = 0; i < str.length; i++) {
			if (str[i].equals(JButtonActionString))
				return i;
		}

		return -1;
	}

	/**
	 * 根据操作符来选择相应的操作
	 * 
	 * @param criteria
	 * @param hp
	 */
	public static void operator(Criteria criteria, HqlParam hp) {
		switch (hp.getOperator()) {
		case 1:
			criteria.add(Restrictions.gt(hp.getKey(), hp.getValue()));
			break;
		case 2:
			criteria.add(Restrictions.lt(hp.getKey(), hp.getValue()));
			break;
		case 3:
			criteria.add(Restrictions.ge(hp.getKey(), hp.getValue()));
			break;
		case 4:
			criteria.add(Restrictions.le(hp.getKey(), hp.getValue()));
			break;
		case 5:
			criteria.add(Restrictions.ilike(hp.getKey(), hp.getValue()));
			break;

		default:
			break;
		}
	}

	public static String dateToString(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(d);
	}

	public static Date getDate(String date) {
		try {
			return new SimpleDateFormat("yyyy-mm-dd").parse(date);
		} catch (ParseException e) {
			System.out.println("解析日期时间出错");
		}
		return null;
	}

	/**
	 * 获取图片
	 * 
	 * @param filePath
	 * @param desc
	 * @return
	 * @throws Exception
	 */
	public static ImageIcon getImageIcon(String filePath, String desc) {
		File file = new File(filePath);
		BufferedImage bufferImage;
		try {
			if (file.exists()) {
				bufferImage = ImageIO.read(file);
				Image item = bufferImage.getScaledInstance(IMAGEWIDTH,
						IMAGEHEIGHT, BufferedImage.SCALE_SMOOTH);
				return new ImageIcon(item, desc);
			}

		} catch (IOException e) {
			System.out.println("加载图片失败");
			return null;
		}
		return null;
	}
	

	/**
	 * 获取剪切板中的内容
	 * 
	 * @return 剪切板中的内容
	 * @throws Exception
	 */
	public static String getClipboardText() throws Exception {

		Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();

		// 获取剪切板中的内容
		Transferable clipT = clipBoard.getContents(null);
		if (clipT != null) {
			// 检查内容是否是文本类型
			if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
				return (String) clipT.getTransferData(DataFlavor.stringFlavor);
		}
		return null;
	}

	public static void openWebByURL(String url) throws Exception {
		URI uri = new URI(url);
		java.awt.Desktop.getDesktop().browse(uri);
	}
}
